#include "PrecompiledHeader_Engineer.h"
#include "CMainMenu.h"

/*--------------------------------------------------------------------------*/

CMainMenu::CMainMenu() :
	EngMod("CMainMenu", 0),
	IGameMode("mainmenu")
{
	this->m_spheres = NULL;
	this->m_spheresCount = 100;

	this->m_lights = NULL;
	this->m_lightsCount = 200;

	this->m_cam3d = NULL;
}

/*--------------------------------------------------------------------------*/

CMainMenu::~CMainMenu()
{

}

/*--------------------------------------------------------------------------*/

void CMainMenu::OnCalc(unsigned int threadId)
{
	float time = (float) CSdl::getInstance()->GetTicks() + 60000;

	if (this->m_spheres != NULL)
	{
		for (unsigned int i = 0; i < this->m_spheresCount; ++i)
		{
			float c = glm::cos(i * time * this->m_spheres[i]->GetRotation().x / 500000.0f);
			float s = glm::sin(i * time * this->m_spheres[i]->GetRotation().z / 500000.0f);

			glm::vec3 v = glm::vec3(s, (((float)i - this->m_spheresCount / 2.0f) / this->m_spheresCount / 2.0f) * 2.0f, c);
			v = glm::normalize(v);

			this->m_spheres[i]->SetPosition(v * 200.0f);
		}
	}

	if (this->m_lights != NULL)
	{
		for (unsigned int i = 0; i < this->m_lightsCount; ++i)
		{
			float c = glm::cos(i * time / 500000.0f);
			float s = glm::sin(i * time / 500000.0f);

			glm::vec3 v = glm::vec3(c, (((float)i - this->m_lightsCount / 2.0f) / this->m_lightsCount / 2.0f) * 2.0f, s);
			v = glm::normalize(v);

			this->m_lights[i]->SetPosition(v * 175.0f);
		}
	}

	if (this->m_cam3d != NULL)
	{
		this->m_cam3d->RotateY(0.2f);
		this->m_cam3d->RotateY(0.2f);
	}
}

/*--------------------------------------------------------------------------*/

void CMainMenu::OnStart()
{
	this->m_cam3d = CCamera::getInstance()->GetFCamera();

	if (this->m_spheres != NULL)
	{
		LogE(1) << "Possibly memory leak";
	}

	Mesh* mesh = NULL;
	CResources::getInstance()->Get("rock", &mesh);

	this->m_spheres = new MeshInstance*[this->m_spheresCount];

	for (unsigned int i = 0; i < this->m_spheresCount; ++i)
	{
		this->m_spheres[i] = mesh->CreateInstance();
		this->m_spheres[i]->SetPosition(glm::vec3(rand() % 200 - 100, rand() % 200 - 100, rand() % 200 - 100));
		this->m_spheres[i]->SetRotation(glm::vec3(rand() % 100, rand() % 100, rand() % 100));
		this->m_spheres[i]->SetRadius((float)(rand() % 30 + 3));
		this->m_spheres[i]->SetScale(this->m_spheres[i]->GetRadius());
		this->m_spheres[i]->SetParent(CTree::getInstance()->GetRoot());
		this->m_spheres[i]->SetName("sferka_z_menu");
	}

	LogI(1) << "Spheres created";

	if (this->m_lights != NULL)
	{
		LogE(1) << "Possibly memory leak";
	}

	this->m_lights = new PointLight*[this->m_lightsCount];

	for (unsigned int i = 0; i < this->m_lightsCount; ++i)
	{
		this->m_lights[i] = new PointLight();
		this->m_lights[i]->SetSpecular(Color(1.0f, 1.0f, 1.0f, 1.0f));
		this->m_lights[i]->SetAmbient(Color(rand() % 256, rand() % 256, rand() % 256, 255));
		this->m_lights[i]->SetDiffuse(this->m_lights[i]->GetAmbient());
		this->m_lights[i]->SetRadius((float)(rand() % 40 + 10));
		this->m_lights[i]->SetPosition(glm::vec3(rand() % 200 - 100, rand() % 200 - 100, rand() % 200 - 100));
		this->m_lights[i]->SetParent(CTree::getInstance()->GetRoot());
		this->m_lights[i]->SetName("swiatelko_z_menu");
	}

	LogI(1) << "Lights created";
}

/*--------------------------------------------------------------------------*/

void CMainMenu::OnStop()
{
	for (unsigned int i = 0; i < this->m_spheresCount; ++i)
	{
		CTree::getInstance()->DeleteObject(this->m_spheres[i]);
	}

	delete [] this->m_spheres;
	this->m_spheres = NULL;
	LogI(1) << "Spheres deleted";

	for (unsigned int i = 0; i < this->m_lightsCount; ++i)
	{
		CTree::getInstance()->DeleteObject(this->m_lights[i]);
	}

	delete [] this->m_lights;
	this->m_lights = NULL;
	LogI(1) << "Lights deleted";

	if (CTree::getInstance()->IsTreeEmpty() == false)
	{
		LogC(1) << "Tree is not empty after CMainMenu::OnStop";
		CShell::getInstance()->Execute("treelog()");
	}
	else
	{
		LogI(1) << "CMainMenu stopped correctly";
	}
}

/*--------------------------------------------------------------------------*/

void CMainMenu::OnInit()
{
	this->m_spheres = NULL;
	this->m_lights = NULL;
}

/*--------------------------------------------------------------------------*/
